package services

import (
    "context"
    "gitee.com/go-libs/db-xorm/db"
    "integration/app/models"
)

func (o *ErpRefundService) GetByOrderId(ctx context.Context, oid int) (model *models.ErpRefund, err error) {
    var (
        has  bool
        sess *db.Session
    )

    if sess, err = o.Slave(ctx); err != nil {
        return
    }

    model = &models.ErpRefund{}

    if has, err = sess.Where("`order_id` = ?", oid).Get(model); err != nil || !has {
        model = nil
    }
    return
}

func (o *ErpRefundService) SetOutIdByOutTradeNo(ctx context.Context, oid int, otn string) (affects int64, err error) {
    var (
        sess  *db.Session
        model = &models.ErpRefund{OutId: oid}
    )

    if sess, err = o.Master(ctx); err != nil {
        return
    }

    affects, err = sess.Cols("out_id").
        Where("`out_trade_no` = ?", otn).
        Update(model)
    return
}
